package com.doubletick.web.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.doubletick.common.core.domain.AjaxResult;
import com.doubletick.common.core.redis.RedisLock;
import com.doubletick.common.utils.ip.IpUtils;
import com.doubletick.proxy.ipidea.domain.ProxyAccountFlow;
import com.doubletick.proxy.ipidea.service.IProxyAccountFlowService;
import com.doubletick.proxy.liao.HttpTest;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/public-api")
public class PublicApi {

    @RequestMapping("/ip")
    public AjaxResult ip() {
        JSONObject json = new JSONObject();
        json.put("ipaddr", IpUtils.getIpAddr());
        json.put("hostIp", IpUtils.getHostIp());
        return AjaxResult.success(json);
    }


    @SneakyThrows
    @RequestMapping("/ppfly")
    public String ppfly(String requestUrl) {
        log.info("ppfly");
        String result = HttpTest.getResponseWithHttpProxy("bra.ppfly.xyz", 1081,
                "feng005_c1-res-US-session-74720844-sessTime-5", "feng005",
                StrUtil.isEmpty(requestUrl) ? "https://api.ipify.org/?format=json" : requestUrl);
//                "https://cip.cc");
        log.info(result);
        return "ppfly:" + result;
    }

    @SneakyThrows
    @RequestMapping("/ipidea")
    public String ipidea(String requestUrl) {
        log.info("ipidea");
        String result = HttpTest.getResponseWithHttpProxy("995eaa21dae5c57f.fkz.as.ipidea.online", 2333,
                "ipidea_june-zone-custom-region-us-session-inqsb2yhe-sessTime-5", "ipidea_june",
                StrUtil.isEmpty(requestUrl) ? "https://api.ipify.org/?format=json" : requestUrl);
//                "https://cip.cc");
        log.info(result);
        return "ipidea:" + result;
    }

    @RequestMapping("/hello")
    public String hello() {
        log.info("hello");
        return "hello";
    }

    @Autowired
    private RedisLock redisLock;

    @RequestMapping("/test")
    public String test() {
        new Thread(() -> {
            boolean lock = redisLock.tryLock("test", 10, TimeUnit.SECONDS);
//            boolean lock = redisLock.tryLock("test",6000, 10, TimeUnit.SECONDS);
            log.info("1-lock:{}:{}", lock, redisLock.isHeldByCurrentThread("test"));
            if (!lock) {
                redisLock.unlock("test");
            }
            try {
                Thread.sleep(3 * 1000L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            redisLock.unlock("test");
            log.info("1-lock:{}-finish", lock);
        }).start();


        new Thread(() -> {
            boolean lock = redisLock.tryLock("test", 10, TimeUnit.SECONDS);
//            boolean lock = redisLock.tryLock("test", 6000,10, TimeUnit.SECONDS);
            log.info("2-lock:{}:{}", lock, redisLock.isHeldByCurrentThread("test"));
            if (!lock) {
                redisLock.unlock("test");
            }
            try {
                Thread.sleep(3 * 1000L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            redisLock.unlock("test");
            log.info("2-lock:{}-finish", lock);

        }).start();


        return "hello";
    }

    @Autowired
    private IProxyAccountFlowService proxyAccountFlowService;

    @RequestMapping("/countIdea")
    public AjaxResult countIdea(String date) {
        if (StrUtil.isBlank(date)) {
            return AjaxResult.error("date is null");
        }
        return AjaxResult.success(proxyAccountFlowService.countByProxyName(date));
    }

    @RequestMapping("/ideaDetail")
    public AjaxResult ideaDetail(String date) {
        if (StrUtil.isBlank(date)) {
            return AjaxResult.error("date is null");
        }
        ProxyAccountFlow query = new ProxyAccountFlow();
        query.getParams().put("beginDataTime", date);
        query.getParams().put("endDataTime", date);
        return AjaxResult.success(proxyAccountFlowService.selectProxyAccountFlowListNoPer(query));
    }

}
